use crate::dto::{
    dept::{DeptList, DeptListPayload, DeptModifyPayload},
    match_no_res_ok, match_ok, Res, ResObj,
};
use crate::service::dept_service;
use hypers::prelude::*;

pub struct Dept;

#[openapi(prefix = "/system", name = "/dept", tag = "部门")]
impl Dept {
    /// 部门列表
    #[get(
        "/list",
        parameter(
            DeptListPayload
        ),
        response(
            (status = 200,body=ResObj<Vec<DeptList>>,description ="部门列表")

        ),
    )]
    async fn get_dept_list(payload: DeptListPayload) -> Res<Vec<DeptList>> {
        match_ok(dept_service::get_dept_list(payload.dept_name, payload.status).await)
    }

    /// 添加部门
    #[post(
        "/",
        response(
            (status = 200,body=ResObj<()>,description ="添加部门")
        ),
    )]
    async fn post_add_dept(payload: Json<DeptModifyPayload>, depot: &mut Request) -> Res<()> {
        let user_id = depot.get::<i32>("userId").unwrap();
        match_no_res_ok(
            dept_service::add_dept(
                user_id,
                payload.dept_name.clone(),
                payload.order_num,
                payload.parent_id,
                payload.leader.clone(),
                payload.email.clone(),
                payload.phone.clone(),
                payload.status.clone(),
            )
            .await,
        )
    }

    /// 修改部门
    #[put(
        "/",
        response(
            (status = 200,body=ResObj<()>,description ="修改部门")
        ),
    )]
    async fn put_edit_dept(payload: Json<DeptModifyPayload>, depot: &mut Request) -> Res<()> {
        let user_id = depot.get::<i32>("userId").unwrap();
        match_no_res_ok(
            dept_service::edit_dept(
                user_id,
                payload.dept_id,
                payload.dept_name.clone(),
                payload.order_num,
                payload.parent_id,
                payload.leader.clone(),
                payload.email.clone(),
                payload.phone.clone(),
                payload.status.clone(),
            )
            .await,
        )
    }

    /// 部门详情
    #[get(
        "/:id",
        response(
            (status = 200,body=ResObj<Option<DeptList>>,description ="部门详情")
        ),
    )]
    async fn get_dept_by_id(id: Path<i64>) -> Res<Option<DeptList>> {
        match_ok(dept_service::get_dept_by_id(id.0).await)
    }

    /// 删除部门
    #[delete(
        "/:id",
        response(
            (status = 200,body=ResObj<()>,description ="删除部门")
        ),
    )]
    async fn del_dept_by_id(id: Path<String>) -> Res<()> {
        match_no_res_ok(dept_service::del_dept_by_id(id.0).await)
    }

    /// 去除传入id部门的部门列表
    #[get(
        "/list/exclude/:id",
        response(
            (status = 200,body=ResObj<Vec<DeptList>>,description ="去除传入id部门的部门列表")
        ),
    )]
    async fn get_dept_list_exclude_id(id: Path<i64>) -> Res<Vec<DeptList>> {
        match_ok(dept_service::get_dept_list_exclude_id(id.0).await)
    }
}
